package com.dai.zhenfujin.visitingcard.app;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.utils.PoiSearchTask;
import com.dai.zhenfujin.visitingcard.adapter.PoiAdapter;
import com.dai.zhenfujin.visitingcard.adapter.SearchResultAdapter;
import com.dai.zhenfujin.visitingcard.app.bean.LocationBean;

public class LocationSearchActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private TextView tvSearch,tvCancel;
    private EditText etSearch;
    private ListView listView;
    private ImageView ivX;
    private String data;
    private String city;
    private SearchResultAdapter searchResultAdapter;
    private PoiAdapter adapter;
    private double latitude,longitude;
    private Object item;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     //   setContentView(R.layout.activity_searchresult);
        setContentView(R.layout.activity_searchresult);
     //   setTransparent();
        tvSearch = (TextView)findViewById(R.id.tv_cancel);
        etSearch = (EditText) findViewById(R.id.et_input);
        listView = (ListView)findViewById(R.id.listView);
        ivX = (ImageView) findViewById(R.id.iv_x);
        tvCancel = findViewById(R.id.tv_cancel);
        city = getIntent().getStringExtra("city");
        latitude = getIntent().getDoubleExtra("latitude",0);
        longitude =  getIntent().getDoubleExtra("longitude",0);
        ivX.setOnClickListener(this);
        tvCancel.setOnClickListener(this);


        adapter = new PoiAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    ivX.setVisibility(View.GONE);
                } else {
                    ivX.setVisibility(View.VISIBLE);
                    //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
                    String eInput =  etSearch.getText().toString();
                    PoiSearchTask.getInstance(LocationSearchActivity.this).setAdapter(adapter).onSearchText(eInput, "");

                   /* InputtipsQuery inputquery = new InputtipsQuery(eInput, "");
                //    inputquery.setCityLimit(false);//限制在当前城市
                    Inputtips inputTips = new Inputtips(LocationSearchActivity.this, inputquery);

                    inputTips.setInputtipsListener(new Inputtips.InputtipsListener() {
                        @Override
                        public void onGetInputtips(List<Tip> list, int i) {
                            searchResultAdapter = new SearchResultAdapter((ArrayList<Tip>) list, LocationSearchActivity.this);
                            listView.setAdapter(searchResultAdapter);
                            searchResultAdapter.notifyDataSetChanged();
                        }
                    });
                    inputTips.requestInputtipsAsyn();*/
                }
            }
        });
    }

    private void setTransparent() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题头
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_searchresult);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
        //窗口对齐屏幕宽度
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams layoutParams = win.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.BOTTOM;//设置对话框在底部显示
        win.setAttributes(layoutParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.iv_x:
                etSearch.setText("");
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LocationBean item = (LocationBean) adapter.getItem(position);
        Intent intent = new Intent();
        intent.putExtra("item", item);
        setResult(1, intent);
        finish();
    }
}
